/**
 * 
 */
package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Supplier;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.SupplierService;

/**
 * @author xiaolongwei@gmail.com
 *
 */
@Controller
@RequestMapping(value = "/supplier")
public class SupplierController {
	@Resource(name="supplierService", type=SupplierService.class)
	private SupplierService supplierService;
	@RequestMapping(value = "/save.do")
	public ModelAndView save(Supplier supplier) {
		supplier.setStatus(BaseInfoStatus.NORMAL);
		supplierService.add(supplier);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("supplier");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView("supplier/add");
		return mv;
	}
	@RequestMapping(value = "/update")
	public ModelAndView update(Supplier supplier) {
		Supplier s = supplierService.getObject(supplier.getId());
		supplier.setStatus(s.getStatus());
		supplierService.modify(supplier);
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("supplier");
		ModelAndView mv = new ModelAndView("dwzresponse");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/edit/{id}.do")
	public  ModelAndView  edit(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("supplier/edit");
		Supplier supplier = supplierService.getObject(id);
		mv.addObject("supplier", supplier);
		return mv;
	}
	@RequestMapping(value = "/delete/{id}.do")
	public ModelAndView delete(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		Supplier s = supplierService.getObject(id);
		s.setStatus(BaseInfoStatus.DELETED);
		supplierService.modify(s);
	//	mv.addObject("stock", StockManager.pop());
		DwzResponse resonse = new DwzResponse();	
		resonse.setNavTabId("supplier");
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/find/{id}")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("index/index");
	//	mv.addObject("stock", StockManager.pop());
		return mv;
	}
	@RequestMapping(value = "/index.do")
	public ModelAndView index(Pager pager) {
		ModelAndView mv = new ModelAndView("supplier/supplierIndex");	
		List<Supplier> suppliers = supplierService.getAll(new HashMap(), pager);
		mv.addObject("suppliers", suppliers);
		mv.addObject("pager", pager);
		return mv;
	}
	
	@RequestMapping("/lookuplist.do")
	public ModelAndView lookuplist(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		ModelAndView mv = new ModelAndView("supplier/supplier_lookup");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = supplierService.getCount(params);
		pager.setTotalCount(totalCount);
		List<Supplier> suppliers = supplierService.getAll(params, pager);
		mv.addObject("suppliers", suppliers);
		return mv;
	}
}
